package cn.uce.cc.ops.job;

import cn.uce.common.constants.CcConstants;
import cn.uce.common.omg.dao.impl.ICsFspDictDateDao;
import cn.uce.elastic.job.base.UceDataflowJob;
import cn.uce.elastic.job.base.UceSimpleJob;
import cn.uce.ics.base.entity.cs.IcsFspDictDataEntity;
import cn.uce.ics.base.entity.cs.RpCallalldata;
import cn.uce.ics.base.vo.srv.IcsPddOrderVo;
import cn.uce.ics.base.vo.srv.IcsRpCallalldataVo;
import cn.uce.ics.base.vo.srv.postal.PotevioTeleReceiveRateReq;
import cn.uce.job.api.service.biz.IRpCallalldataBiz;
import cn.uce.job.entity.CallDbEnums;
import cn.uce.web.authorg.dao.IDictDataDao;
import cn.uce.web.authorg.entity.DictData;
import cn.uce.web.common.util.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.google.common.collect.Lists;
import org.apache.commons.lang.time.DateUtils;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 话务接听数据拉取流式拉取批
 * 1.读取分片配置同步时间，同步历史数据,一次查询200条
 * 2.默认按天同步历史数据，直到同步到当前时间
 * 3.当前时间配置30分钟同步一次
 * 4.同步一次更新下次分片同步时间
 * @author Sunny
 * @date 2018-12-04
 */
public class CallDbToCcJob extends UceSimpleJob {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Resource
    private IRpCallalldataBiz rpCallalldataBiz;

    @Resource
    private ICsFspDictDateDao csFspDictDateDao;
    @Resource
    private IDictDataDao dictDataDao;

//    public List<IcsRpCallalldataVo> fetchData(ShardingContext shardingContext) {
//        logger.info("CallDbToCcJob分片总数：" + shardingContext.getShardingTotalCount() + " fetchData分片项: "
//                + shardingContext.getShardingItem() + "线程ID: " + Thread.currentThread().getId());
//        PotevioTeleReceiveRateReq vo = new PotevioTeleReceiveRateReq();
//        vo.setShardingItem(shardingContext.getShardingItem());
//        vo.setShardingTotalCount(shardingContext.getShardingTotalCount());
//        // 未开启则不继续执行
//        if(!rpCallalldataBiz.getThirdDbToCcDbSwitch()){
//            return null;
//        }
//
//        IcsRpCallalldataVo info =  getData(vo,shardingContext);
//        if(org.springframework.util.ObjectUtils.isEmpty(info)){
//            return null;
//        }
//        return Lists.newArrayList(info);
//    }
//
//    /**
//     * 切片处理
//     * @param data
//     * @param formIndex
//     * @param toIndex
//     * @return
//     */
//    public List<IcsPddOrderVo> subList(List<IcsPddOrderVo> data,int formIndex,int toIndex){
//        return data.subList(formIndex,toIndex);
//    }
//
    /**
     * 获取数据集
     * @param vo
     * @return
     */
    public IcsRpCallalldataVo getData(PotevioTeleReceiveRateReq vo,ShardingContext shardingContext){
        List<RpCallalldata> data = Lists.newArrayList();
        IcsRpCallalldataVo res = new IcsRpCallalldataVo();
        try{
            DateTime dateTime = new DateTime(new Date());
            SimpleDateFormat formatter = new SimpleDateFormat( DateUtil.DATE_PATTERN.YYYY_MM_DD_HH_MM_SS);
            Date beginTimeday = dateTime.minusDays(1).withMillisOfDay(0).toDate();
            vo.setStarttime(formatter.format(beginTimeday));
            Date endTimeday = dateTime.minusDays(0).withMillisOfDay(0).toDate();
            vo.setEndtime(formatter.format(endTimeday));
            logger.info("话务工单分片{}拉取入口参数:{}",shardingContext.getShardingItem(),JSONObject.toJSONString(vo));
            data = rpCallalldataBiz.findOrcaleByList(vo);
            if(org.springframework.util.ObjectUtils.isEmpty(data)){
                return null;
            }
            vo.setTotal(data.size());
            logger.info("话务工单分片{}拉取完成:{}",shardingContext.getShardingItem(),JSONObject.toJSONString(vo)+"size"+data.size());
            res.setData(data);
        }catch (Exception e){
            logger.error("话务工单拉取异常时间不更新，等待下一次拉取:",e);
            return null;
        }
        return res;
    }
//
//    @Override
//    public void processData(ShardingContext shardingContext, List<IcsRpCallalldataVo> reqs) {
//        IcsRpCallalldataVo req = reqs.get(0);
//        List<RpCallalldata> data = req.getData();
//        logger.debug("分片项[{}]-["+shardingContext.getJobName()+"]processDataStart ===============================",shardingContext.getShardingItem());
//        logger.info("processData分片项[{}]-线程ID["+Thread.currentThread().getId()+"]",shardingContext.getShardingItem());
//        logger.info("processData分片项[{}]-DataCount["+data.size()+"]",shardingContext.getShardingItem());
//        Map<String, Object> map = new HashMap<String, Object>(CcConstants.HASH_MAP_LENGTH);
//        rpCallalldataBiz.insert(data,map);
//    }

    @Override
    public void doExecute(ShardingContext shardingContext) {
        logger.info("CallDbToCcJob分片总数：" + shardingContext.getShardingTotalCount() + " fetchData分片项: "
                + shardingContext.getShardingItem() + "线程ID: " + Thread.currentThread().getId());
        PotevioTeleReceiveRateReq vo = new PotevioTeleReceiveRateReq();
        vo.setShardingItem(shardingContext.getShardingItem());
        vo.setShardingTotalCount(shardingContext.getShardingTotalCount());
        // 未开启则不继续执行
        if(!rpCallalldataBiz.getThirdDbToCcDbSwitch()){
            return  ;
        }
        IcsRpCallalldataVo info =  getData(vo,shardingContext);
        if(org.springframework.util.ObjectUtils.isEmpty(info)){
            return  ;
        }
        List<RpCallalldata> data = info.getData();
        logger.debug("分片项[{}]-["+shardingContext.getJobName()+"]processDataStart ===============================",shardingContext.getShardingItem());
        logger.info("processData分片项[{}]-线程ID["+Thread.currentThread().getId()+"]",shardingContext.getShardingItem());
        logger.info("processData分片项[{}]-DataCount["+data.size()+"]",shardingContext.getShardingItem());
        Map<String, Object> map = new HashMap<String, Object>(CcConstants.HASH_MAP_LENGTH);
        rpCallalldataBiz.insert(data,map);


    }
}
